<?php

/**
 * This file is part of the Codizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Codizer Core Team <team@codizer.org>
 * @package     Codizer
 */

class f_xml_XmlElement extends SimpleXMLElement
{
	private $disableI18n = false;
	
	/**
	 * Disable locale auto resolve 
	 *
	 * @return f_xml_XmlElement
	 */
	public function disableI18n()
	{
		$this->disableI18n = true;
		return $this;
	}
	
	/**
	 * Get XML property attributes
	 *
	 * @param string $attributeName
	 * @return f_xml_Element
	 */
	public function getAttributes()
	{
		$result = array();
		$attributes = get_object_vars($this->attributes());
		$attributes = end($attributes);
		
		if(is_array($attributes))
		{
			foreach($attributes as $key => $attribute)
			{				
				if(strstr($attribute, "locale.") && $this->disableI18n == false)
				{
                    $lang = f_core_Context::getInstance()->getDisplayLang();
                    $lang = is_null($lang) ? f_builder_TranslationParser::$DEFAULT_TPL_LANG:$lang;
					$result[$key] = f_locale_Manager::getLocale($attribute, $lang);
				}
				else
				{
					$result[$key] = $attribute;
				}
			}			
		}
		return $result;
	}
	
	/**
	 * Get attribute value from element with given attribute name
	 *
	 * @param string $attributeName
	 * @return string|null
	 */
	public function getAttributeValue($attributeName = null, $defaultValue = null)
	{
		if(!is_null($attributeName))
		{
			$attributes = $this->getAttributes();
			
			if(is_array($attributes) && isset($attributes[$attributeName]))
			{
				if(strstr($attributes[$attributeName], "locale.")  && $this->disableI18n == false)
				{
                    $lang = f_core_Context::getInstance()->getDisplayLang();
                    $lang = is_null($lang) ? f_builder_TranslationParser::$DEFAULT_TPL_LANG:$lang;
					return f_locale_Manager::getLocale($attributes[$attributeName], $lang);
				}
				return $attributes[$attributeName];	
			}			
			else
			{
				return $defaultValue;
			}
		}
		return $defaultValue;
	}

	/**
	 * Get all subelements with the given xpath
	 *
	 * @param string $propertyName
	 * @return f_xml_XmlElement
	 */
	public function getSubElement($xpath = null)
	{
		if(!is_null($xpath))
		{
			if(count($this->xpath($xpath)) == 0)
			{
				return null;
			}

            $ar = $this->xpath($xpath);
			return $ar[0];
		}
		return null;
    }

	/**
	 * Get all subelements with the given xpath
	 *
	 * @param string $propertyName
	 * @return array|null
	 */
	public function getSubElements($xpath = null)
	{
		if(!is_null($xpath))
		{
			if(count($this->xpath($xpath)) == 0)
			{
				return null;
			}
			
			return $this->xpath($xpath);
		}
		return null;
	}	

	/**
	 * Return content between current xpath tags
	 *
	 * @return SimpleXMLElement
	 */
	public function getContent()
	{
        $content = end($this);
        if(is_string($content) && strstr($content, "locale.") && $this->disableI18n == false)
        {
            $lang = f_core_Context::getInstance()->getDisplayLang();

            $lang = is_null($lang) ? f_builder_TranslationParser::$DEFAULT_TPL_LANG:$lang;
            $content = f_locale_Manager::getLocale($content, $lang);
        }
		return $content;
	}

    /**
     * Return current object vars
     *
     * @return array
     */
    public function toArray()
    {
        return get_object_vars($this);
    }
}